<?php

class Nimblecms_Module_Loader {
	
	private static $_instance;
	
	private $_moduleNames;
	
	public static function getInstance() {
		if (null == self::$_instance) {
			self::$_instance = new self();
		}
		return self::$_instance;
	}
	
	private function __construct() 
	{
		$this->_moduleNames = $this->_getModules();
	}
	
	public function getModuleNames() 
	{
		return $this->_moduleNames;
	}
	
	/**
	 * Получает список активированных плагинов
	 */
	public function getActivePlugins() {
		return $this->_getActivePlugins();
	}
	
	/**
	 * 
	 * Получает роуты
	 */
	public function getRoutes() {
		return $this->_getRoutes();
	}
	
	private function _getRoutes() {
		if(null == $this->_moduleNames) {
			return null;
		}
		
		$router = new Zend_Controller_Router_Rewrite();
		
		foreach ($this->_moduleNames as $name) {
			$configFiles = $this->_loadRouteConfigs($name);
			
			foreach ($configFiles as $file) {
				$config = new Zend_Config_Ini($file, 'routes');
				$router->addConfig($config, 'routes');
			}
		}
		
		return $router;
	}
	
	/**
     * Загружает файлы конфигурации модуля
	 * @param string $name имя модуля
	 */
	private function _loadRouteConfigs($name) {
		$dir = APPLICATION_PATH . DS . 'modules' . DS . $name . DS . 'configs' . DS . 'routes';
		
		if(!is_dir($dir)) {
			
			return array();
		}
		
		$configFiles = array();
		$dirIterator = new DirectoryIterator($dir);
		
		foreach($dirIterator as $file) {
			if($file->isDot() || $file->isDir()) {
				continue;
			}
			
			$filename = $file->getFilename();
			
			if (preg_match('/^[^a-z]/i', $filename) || ('CVS' == $filename) 
            		|| ('.svn' == strtolower($filename))) {
                continue;
            }
            
            if (preg_match('/^[^a-z]/i', $filename) || ('CVS' == $filename) 
            		|| ('.svn' == strtolower($filename))) {
                continue;
            }
            
            $configFiles[] = $dir . DS . $filename;
		}
		
		
		return $configFiles;
	}
	
	private function _getModules() 
	{
		$dir = APPLICATION_PATH . DIRECTORY_SEPARATOR . "modules";
		
		if (!file_exists($dir)) {
			return array();
		}
		
		$subDirs 	 = array();
		$dirIterator = new DirectoryIterator($dir);
		foreach ($dirIterator as $dir) {
            if ($dir->isDot() || !$dir->isDir()) {
                continue;
            }
            $dir = $dir->getFilename();
            if ($dir == '.svn') {
            	continue;
            }
            $subDirs[] = $dir;
		}
		return $subDirs;
	}
	
	
	
	private function _getActivePlugins() {
		$dir = APPLICATION_PATH . DS . "plugins/";
		
		if(!file_exists($dir)) {
			
			return array();
		}
			
		$dirIterator = new DirectoryIterator($dir);
               
		foreach ($dirIterator as $dir) {
                    if ($dir->isDot() || !$dir->isDir()) {
                        continue;
                    }
                    $dir = $dir->getFilename();
                    if ($dir == '.svn') {
                        continue;
                    }
                    $subDirs[] = $dir;
                    
		}
              
		
		$enabledPlugins = array();
		
		foreach($subDirs as $plugin) {
			$file = APPLICATION_PATH . DS . "plugins" . DS . $plugin . DS . "config.xml";
			
			if(!file_exists($file)) {
				
				continue;
			}
			$params = simplexml_load_file($file);
			$xml = $params->xpath("//param[@name='enable']");
			
			if($xml[0]->value == null) {
				continue;
			} else {
				if($xml[0]->value == '1') {
					$enabledPlugins[] = $plugin;
				}
			}
				
		}
		
		return $enabledPlugins;
	}
}
